# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :crde_base_page.py
# @Time      :2023/11/15 10:59
# @Author    :WangXiang

from pages.base_page import BasePage
from selenium.webdriver.common.by import By


class CrdeBasePage(BasePage):
    """crde 基础页面"""

    USER = (By.XPATH, "/html/body/div[1]/div/div[2]/div[2]/div/div[2]/form/div[1]/div/div[1]/input")
    PASS = (By.XPATH, "/html/body/div[1]/div/div[2]/div[2]/div/div[2]/form/div[2]/div/div[1]/input")
    LOGINBUTTON = (By.CSS_SELECTOR, ".el-button.el-button--primary.login-btn.primary-bj")

    LOGINSUCCESS = (By.XPATH, "/html/body/div[1]/div[1]/div[2]/div/div[3]/div/div[1]/div[1]/button[1]/span")

    MENU = (By.CSS_SELECTOR, ".slider-title")

    def login(self, url, username, password):
        """登录"""
        self.open(url, "打开智诚官网首页")
        self.click_element_by_text("欢迎登录", "点击欢迎登录")
        self.sleep(0.5)
        self.click_element_by_text("密码登录", "切换密码登录")
        self.sendKeys(self.USER, username,
                      "输入用户名:{}".format(username))

        self.sendKeys(self.PASS, password,
                      "输入密码:{}".format(password))

        self.click(self.LOGINBUTTON, "点击登录按钮")
        self.sleep(0.5)

    def menu(self, menu_name, two_menu_name=None):
        """
            crde 左侧菜单
            menu_name： 一级菜单名称
            two_menu_name： 二级菜单名称， 没有可用传入
        """
        elements = self.get_elements(By.XPATH, "/html/body/div[1]/div[1]/div[1]/div[1]/div[2]/div")
        for i in range(0, len(elements)):
            menu_location = (By.XPATH, "/html/body/div[1]/div[1]/div[1]/div[1]/div[2]/div[{}]".format(i + 1))
            if self.text_value(menu_location) == menu_name:
                self.click(menu_location, "点击一级目录:{}".format(menu_name))
                if two_menu_name:
                    two_menu_location = (
                        By.XPATH, "/html/body/div[1]/div[1]/div[1]/div[1]/div[2]/div[{}]".format(i + 1) + "/div[2]")
                    if self.assert_element_exits(two_menu_location):  # 如果传入了二级目录名称
                        elements_a = self.get_elements(By.XPATH,
                                                       "/html/body/div[1]/div[1]/div[1]/div[1]/div[2]/div[{}]".format(
                                                           i + 1) + "/div[2]/a")
                        for ii in range(0, len(elements_a)):
                            a_location = (By.XPATH, "/html/body/div[1]/div[1]/div[1]/div[1]/div[2]/div[{}]".format(
                                i + 1) + "/div[2]/a[{}]".format(ii + 1))

                            if self.text_value(a_location) == two_menu_name:
                                self.click(a_location, "点击二级目录:{}".format(two_menu_name))
                    else:
                        print("没有二级目录")
                else:
                    print("不需要点击二级目录")
                    break

    def select_enterprise(self, enterprise_name):

        """选择企业"""
        elements = self.get_elements(By.XPATH, "/html/body/div/div[1]/div")
        for i in range(0, len(elements)):
            enterprise_location = (
                By.XPATH, "/html/body/div/div[1]/div[{}]".format(i + 2) + "/div/div/div[1]/div[2]/div[1]/h6")
            if self.text_value(enterprise_location) == enterprise_name:
                self.click(enterprise_location, "进入企业:{}".format(enterprise_name))
                break

